package org.dromara.school.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.school.listener.DisciplineImportExcelListener;
import org.dromara.school.service.ICampusService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.school.domain.vo.DisciplineVo;
import org.dromara.school.domain.bo.DisciplineBo;
import org.dromara.school.service.IDisciplineService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学科管理
 *
 * @author Gjb
 * @date 2025-06-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/school/discipline")
public class DisciplineController extends BaseController {

    private final IDisciplineService disciplineService;

    private final ICampusService campusService;

    /**
     * 查询学科管理列表
     */
    @SaCheckPermission("school:discipline:list")
    @GetMapping("/list")
    public TableDataInfo<DisciplineVo> list(DisciplineBo bo, PageQuery pageQuery) {
        return disciplineService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出学科管理列表
     */
    @SaCheckPermission("school:discipline:export")
    @Log(title = "学科管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DisciplineBo bo, HttpServletResponse response) {
        List<DisciplineVo> list = disciplineService.queryList(bo);
        ExcelUtil.exportExcel(list, "学科管理", DisciplineVo.class, response);
    }

    /**
     * 获取学科管理详细信息
     *
     * @param disciplineId 主键
     */
    @SaCheckPermission("school:discipline:query")
    @GetMapping("/{disciplineId}")
    public R<DisciplineVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long disciplineId) {
        return R.ok(disciplineService.queryById(disciplineId));
    }

    /**
     * 新增学科管理
     */
    @SaCheckPermission("school:discipline:add")
    @Log(title = "学科管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DisciplineBo bo) {
        return toAjax(disciplineService.insertByBo(bo));
    }

    /**
     * 修改学科管理
     */
    @SaCheckPermission("school:discipline:edit")
    @Log(title = "学科管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DisciplineBo bo) {
        return toAjax(disciplineService.updateByBo(bo));
    }

    /**
     * 删除学科管理
     *
     * @param disciplineIds 主键串
     */
    @SaCheckPermission("school:discipline:remove")
    @Log(title = "学科管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{disciplineIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] disciplineIds) {
        return toAjax(disciplineService.deleteWithValidByIds(List.of(disciplineIds), true));
    }


    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "学科管理导入模版", DisciplineVo.class, response);
    }





    @PostMapping("/upload")
    public R upload(@RequestPart("file") MultipartFile file) {

        try {
            EasyExcel.read(file.getInputStream(), DisciplineVo.class, new DisciplineImportExcelListener(disciplineService, campusService)).sheet().doRead();
            return R.ok();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.fail("文件导入失败！");
    }
}
